home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / utility / uwserver.zip / uwserver.tar / server / uw_utmp.c < prev    next >
C/C++ Source or Header  |  1991-01-25  |  4KB  |  200 lines

  1. /*
  2.  *    uw_utmp - /etc/utmp handling
  3.  *
  4.  * Copyright 1985,1986 by John D. Bruner.  All rights reserved.  Permission to
  5.  * copy this program is given provided that the copy is not sold and that
  6.  * this copyright notice is included.
  7.  */
  8.  
  9. #ifdef UTMP
  10.  
  11. #include <sys/types.h>
  12. #include <sys/file.h>
  13. #include <sys/time.h>
  14. #include <pwd.h>
  15. #include <utmp.h>
  16. #include <strings.h>
  17. #include <ctype.h>
  18. #include <stdio.h>
  19.  
  20. #include "uw_param.h"
  21.  
  22. struct utinfo {
  23.     struct utinfo    *ui_next;
  24.     struct utinfo    *ui_chain;
  25.     char        *ui_line;
  26.     int        ui_slot;
  27.     int        ui_inuse;
  28. };
  29.  
  30. static struct utinfo *hash[31];
  31. static struct utinfo *head;
  32.  
  33. static char *myname;
  34. static fildes_t utmpfd;
  35.  
  36. extern time_t time();
  37.  
  38. utmp_init(fd)
  39. fildes_t fd;
  40. {
  41.     register char *cp, *cq;
  42.     register struct utinfo *ui;
  43.     register int hashidx, slot;
  44.     struct passwd *pw;
  45.     FILE *fp;
  46.     char line[256];
  47.  
  48.     if ((utmpfd = fd) >= 0 && (fp = fopen("/etc/ttys", "r")) == NULL) {
  49.         (void)close(utmpfd);
  50.         utmpfd = -1;
  51.     }
  52.     if (utmpfd >= 0) {
  53.         slot = 0;
  54.         while (fgets(line, sizeof line, fp) != NULL) {
  55. #ifdef V7TTYS
  56.             if (!line[0] || !line[1]) {    /* malformed line */
  57.                 slot++;
  58.                 continue;
  59.             }
  60.             cp = line+2;    /* skip flag and speed index */
  61. #else
  62.             for (cp=line; *cp && isspace(*cp); cp++)
  63.                 ;
  64.             if (*cp == '#')
  65.                 continue;
  66. #endif
  67.             slot++;
  68.             if ((ui=(struct utinfo *)malloc(sizeof *ui)) != NULL) {
  69.                 for (cq=cp; *cq && !isspace(*cq); cq++)
  70.                     ;
  71.                 if ((ui->ui_line=malloc(cq-cp+1)) != NULL) {
  72.                     (void)strncpy(ui->ui_line, cp, cq-cp);
  73.                     ui->ui_line[cq-cp] = '\0';
  74.                 } else {
  75.                     free((char *)ui);
  76.                     ui = (struct utinfo *)0;
  77.                 }
  78.             }
  79.             if (ui != NULL) {
  80.                 ui->ui_slot = slot;
  81.                 ui->ui_inuse = 0;
  82.                 ui->ui_chain = head;
  83.                 head = ui;
  84.                 hashidx = utmp_hash(ui->ui_line);
  85.                 ui->ui_next = hash[hashidx];
  86.                 hash[hashidx] = ui;
  87.             }
  88.         }
  89.         (void)fclose(fp);
  90.     }
  91.     if ((pw = getpwuid(getuid())) != NULL &&
  92.         (myname=malloc(1+strlen(pw->pw_name))) != NULL)
  93.         (void)strcpy(myname, pw->pw_name);
  94. }
  95.  
  96. static
  97. struct utinfo *
  98. utmp_find(tty)
  99. char *tty;
  100. {
  101.     register char *cp;
  102.     register struct utinfo *ui;
  103.  
  104.     if ((cp = rindex(tty, '/')) != NULL)
  105.         cp++;
  106.     else
  107.         cp = tty;
  108.     ui = hash[utmp_hash(cp)];
  109.     while (ui != NULL && strcmp(ui->ui_line, cp) != 0)
  110.         ui = ui->ui_next;
  111.     return(ui);
  112. }
  113.  
  114. utmp_add(tty)
  115. char *tty;
  116. {
  117.     register struct utinfo *ui;
  118.     struct utmp ut;
  119.  
  120.     if ((ui = utmp_find(tty)) != NULL) {
  121.         (void)strncpy(ut.ut_line, ui->ui_line, sizeof ut.ut_line);
  122.         (void)strncpy(ut.ut_name, myname, sizeof ut.ut_name);
  123.         (void)strncpy(ut.ut_host, "", sizeof ut.ut_host);
  124.         ut.ut_time = (long)time((time_t)0);
  125.         ui->ui_inuse = 1;
  126.         utmp_write(ui->ui_slot, &ut);
  127.     }
  128. }
  129.  
  130.  
  131. utmp_rm(tty)
  132. char *tty;
  133. {
  134.     register struct utinfo *ui;
  135.     struct utmp ut;
  136.  
  137.     if ((ui = utmp_find(tty)) != NULL) {
  138.         (void)strncpy(ut.ut_line, ui->ui_line, sizeof ut.ut_line);
  139.         (void)strncpy(ut.ut_name, "", sizeof ut.ut_name);
  140.         (void)strncpy(ut.ut_host, "", sizeof ut.ut_host);
  141.         ut.ut_time = (long)time((time_t)0);
  142.         ui->ui_inuse = 0;
  143.         utmp_write(ui->ui_slot, &ut);
  144.     }
  145. }
  146.  
  147. utmp_exit()
  148. {
  149.     register struct utinfo *ui;
  150.     struct utmp ut;
  151.  
  152.     for (ui=head; ui; ui=ui->ui_chain) {
  153.         if (ui->ui_inuse) {
  154.             (void)strncpy(ut.ut_line,ui->ui_line,sizeof ut.ut_line);
  155.             (void)strncpy(ut.ut_name, "", sizeof ut.ut_name);
  156.             (void)strncpy(ut.ut_host, "", sizeof ut.ut_host);
  157.             ut.ut_time = (long)time((time_t)0);
  158.             ui->ui_inuse = 0;
  159.             utmp_write(ui->ui_slot, &ut);
  160.         }
  161.     }
  162. }
  163.  
  164. utmp_write(slot, ut)
  165. register int slot;
  166. struct utmp *ut;
  167. {
  168.     extern off_t lseek();
  169.  
  170.     if (utmpfd >= 0 &&
  171.         lseek(utmpfd, slot*sizeof(*ut), L_SET) == (off_t)(slot*sizeof(*ut)))
  172.         (void)write(utmpfd, (char *)ut, sizeof *ut);
  173. }
  174.  
  175. static
  176. utmp_hash(s)
  177. register char *s;
  178. {
  179.     register short h;
  180.  
  181.     for (h=0; *s; s++)
  182.         h = (h << ((*s)&7)) | (h >> (sizeof h - ((*s)&7))) + *s;
  183.     return(h % sizeof hash / sizeof hash[0]);
  184. }
  185. #else
  186. utmp_add(tty)
  187. char *tty;
  188. {
  189. }
  190.  
  191. utmp_rm(tty)
  192. char *tty;
  193. {
  194. }
  195.  
  196. utmp_exit()
  197. {
  198. }
  199. #endif
  200.